<!-- 登录注册 -->

<template>
    <div class="login">
        <div class="login-content" v-if="!mobieType">
            <div class="shell">
                <div class="container a-container is-txl" id="a-container">
                    <form action="" method="" class="form" id="a-form">
                        <h2 class="form_title title">创建账号</h2>
                        <span class="form_span">一次性选好，别指望修改哈~</span>
                        <span class="form_span">选择你的头像</span>
                        <div class="avatarList" @click.stop="chooseAvatar" ref="avatarList">
                            <!-- <div @click="handleLeft"><<<</div> -->
                            <img :src="item | getAvatar" v-for="item in 5" :key="item"
                                :class="item === 1 && 'img-active'">
                            <!-- <div @click="handleRight">>>></div> -->
                        </div>
                        <input type="text" class="form_input" placeholder="名称" v-model="signUpForm.username"
                            @blur="validateUsername">
                        <input type="text" class="form_input" placeholder="邮箱（将作为您的账号）" v-model="signUpForm.email"
                            @blur="validateEmail">
                        <input type="text" class="form_input" placeholder="密码" v-model="signUpForm.password"
                            @blur="validatePassword">
                        <input type="text" class="form_input" placeholder="确认密码" v-model="checkPassword"
                            @blur="validateCheckPassword">
                        <button class="form_button button submit" @click.prevent="handleSignUp">注 册</button>
                    </form>
                </div>

                <div class="container b-container is-txl is-z" id="b-container">
                    <form action="" method="" class="form" id="b-form">
                        <h2 class="form_title title">登入账号</h2>
                        <div class="form_icons">
                            <!-- <i class="iconfont icon-QQ"></i>
                        <i class="iconfont icon-weixin"></i>
                        <i class="iconfont icon-bilibili-line"></i> -->
                        </div>
                        <!-- <span class="form_span">选择登录方式活电子邮箱登录</span> -->
                        <input type="text" class="form_input" placeholder="邮箱" v-model="signInForm.email">
                        <input type="text" class="form_input" placeholder="密码" v-model="signInForm.password">
                        <a class="form_link"  @click="forgotPwd">忘记密码？</a>
                        <button class="form_button button submit" @click.prevent="handleSignIn">登 录</button>
                    </form>
                </div>

                <div class="switch is-gx is-txr" id="switch-cnt">
                    <div class="switch_circle is-txr"></div>
                    <div class="switch_circle switch_circle-t is-txr"></div>
                    <div class="switch_container is-hidden" id="switch-c1">
                        <h2 class="switch_title title" style="letter-spacing: 0;">Welcome Back！</h2>
                        <p class="switch_description description">已经有账号了嘛，去登入账号来火力全开吧！！！</p>
                        <button class="switch_button button switch-btn" @click="changeForm">登 录</button>
                    </div>

                    <div class="switch_container" id="switch-c2">
                        <h2 class="switch_title title" style="letter-spacing: 0;">Hello Friend！</h2>
                        <p class="switch_description description">去注册一个账号，成为尊贵的会员，去评论区激情对线吧！</p>
                        <button class="switch_button button switch-btn" @click="changeForm">注 册</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="login-content-mobie" v-else>
            <div class="shell">
                <div class="container a-container is-txl" id="a-container">
                    <button class="mobie-toggle-btn" @click="changeForm">登 录</button>
                    <form action="" method="" class="form" id="a-form">
                        <h2 class="form_title title">创建账号</h2>
                        <span class="form_span">一次性选好，别指望修改哈~</span>
                        <span class="form_span">选择你的头像</span>
                        <div class="avatarList" @click.stop="chooseAvatar" ref="avatarList">
                            <!-- <div @click="handleLeft"><<<</div> -->
                            <img :src="item | getAvatar" v-for="item in 5" :key="item"
                                :class="item === 1 && 'img-active'" :title="123">
                            <!-- <div @click="handleRight">>>></div> -->
                        </div>
                        <input type="text" class="form_input" placeholder="名称" v-model="signUpForm.username"
                            @blur="validateUsername">
                        <input type="text" class="form_input" placeholder="邮箱（将作为您的账号）" v-model="signUpForm.email"
                            @blur="validateEmail">
                        <input type="text" class="form_input" placeholder="密码" v-model="signUpForm.password"
                            @blur="validatePassword">
                        <input type="text" class="form_input" placeholder="确认密码" v-model="checkPassword"
                            @blur="validateCheckPassword">
                        <button class="form_button button submit" @click.prevent="handleSignUp">注 册</button>
                    </form>
                </div>

                <div class="container b-container is-txl is-z" id="b-container">
                    <button class="mobie-toggle-btn" @click="changeForm">注 册</button>
                    <form action="" method="" class="form" id="b-form">
                        <h2 class="form_title title">登入账号</h2>
                        <div class="form_icons">
                            <!-- <i class="iconfont icon-QQ"></i>
                            <i class="iconfont icon-weixin"></i>
                            <i class="iconfont icon-bilibili-line"></i> -->
                        </div>
                        <!-- <span class="form_span">选择登录方式活电子邮箱登录</span> -->
                        <input type="text" class="form_input" placeholder="邮箱" v-model="signInForm.email">
                        <input type="text" class="form_input" placeholder="密码" v-model="signInForm.password">
                        <a class="form_link" @click="forgotPwd">忘记密码？</a>
                        <button class="form_button button submit" @click.prevent="handleSignIn">登 录</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="mask" v-show="forConfirm" @click="handleCancleFor">
            <div class="mask-content">
                <span>请在24小时之内用您的邮箱发送"忘记密码"附带注册时的个人信息至站长邮箱，确定忘记了吗？</span>
                <div class="mask-btn">
                    <button @click="handleForgot">想起来了</button>
                    <button class="del" @click="forConfirm = false">我明白了</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { fetchSignUp, fetchSignIn } from '@/api';
import store from '../store';

export default {
    name: 'Login',
    data() {
        return {
            signUpForm: {
                avatar: '../assets/img/1.jpeg',
                username: '',
                email: '',
                password: ''
            },
            signInForm: {
                email: '',
                password: ''
            },
            checkPassword: '',
            mobieType: false,
            forConfirm:false
        }
    },
    created() {
        const width = document.documentElement.clientWidth || document.body.clientWidth;
        if (width < 900) {
            this.mobieType = true
        }
    },
    methods: {
        changeForm() {
            if (!this.mobieType) {
                let switchCtn = document.querySelector("#switch-cnt");
                let switchC1 = document.querySelector("#switch-c1");
                let switchC2 = document.querySelector("#switch-c2");
                let switchCircle = document.querySelectorAll(".switch_circle");
                let aContainer = document.querySelector("#a-container");
                let bContainer = document.querySelector("#b-container");

                switchCtn.classList.add("is-gx");
                setTimeout(function () {
                    switchCtn.classList.remove("is-gx");
                }, 1500)
                switchCtn.classList.toggle("is-txr");
                switchCircle[0].classList.toggle("is-txr");
                switchCircle[1].classList.toggle("is-txr");

                switchC1.classList.toggle("is-hidden");
                switchC2.classList.toggle("is-hidden");

                aContainer.classList.toggle("is-txl");
                bContainer.classList.toggle("is-txl");
            }


            
            let bContainer = document.querySelector("#b-container");
            bContainer.classList.toggle("is-z");
        },
        chooseAvatar(e) {
            const target = e.target;
            if (target.nodeName == 'IMG') {
                this.$refs.avatarList.childNodes.forEach(val => {
                    val.classList.remove('img-active');
                })
                target.classList.add('img-active');
                this.signUpForm.avatar = Array.from(this.$refs.avatarList.childNodes).indexOf(target)+1;
            };
        },
        validateUsername() {
            if (this.signUpForm.username.length < 2 || this.signUpForm.username.length > 6) {
                this.$message.error('用户名长度为2-6位');
                return false
            }
            return true;
        },
        validateEmail() {
            if (!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(this.signUpForm.email)) {
                this.$message.error('邮箱格式错误');
                return false
            }
            return true;
        },
        validatePassword() {
            if (this.signUpForm.password.length < 6 || this.signUpForm.password.length > 15) {
                this.$message.error('密码长度为6-15位');
                return false
            }
            return true;
        },
        validateCheckPassword() {
            if (this.signUpForm.password != this.checkPassword) {
                this.$message.error('搞笑呢    ？？？？？');
                return false
            }
            return true;
        },
        handleSignUp() {
            const validate = this.validateUsername() && this.validateEmail() && this.validatePassword() && this.validateCheckPassword() // 校验
            if (!validate) return
            fetchSignUp(this.signUpForm).then(res => { // 注册
                if (res === '添加成功') {
                    this.$message.success('注册成功');
                    this.signUpForm = {
                        username: '',
                        email: '',
                        password: '',
                    }
                    this.checkPassword = ''
                    this.changeForm()
                }
            }).catch(err => {
                this.$message.error(err);
            })
        },
        handleSignIn() {
            if (this.signInForm.username === '' || this.signInForm.password === '') {
                return this.$message.error('用户名和密码让我给你输？？')
            }
            fetchSignIn(this.signInForm).then(res => {
                this.$message.success('登录成功');
                this.signInForm = {
                    username: '',
                    password: '',
                }
                store.dispatch('setUserInfo', res)
                localStorage.setItem('ygod-userInfo', JSON.stringify(res))
                this.$router.push('/')
            }).catch(err => {
                this.$message.error(err);
            })
        },
        forgotPwd(){
            this.$message.error('废死了就');
            this.forConfirm = true
        },
        handleCancleFor(e){
            if (e.target.className === 'mask') {
                this.forConfirm = false
            };
        },
        handleForgot(){
            this.$message.error('真汰');
            this.forConfirm = false
        }
    }
}
</script>

<style scoped lang="less">
.shell {
    position: relative;
    left: 50%;
    top: 100px;
    transform: translateX(-50%);
    width: 1000px;
    min-width: 1000px;
    min-height: 600px;
    height: 600px;
    padding: 25px;
    background-color: #ecf0f3;
    box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
    border-radius: 12px;
    overflow: hidden;
    z-index: 100;
}

/* 设置响应式 */
// @media (max-width: 1200px) {
//     .shell {
//         transform: scale(0.7);
//     }
// }

// @media (max-width: 1000px) {
//     .shell {
//         transform: scale(0.6);
//     }
// }

// @media (max-width: 800px) {
//     .shell {
//         transform: scale(0.5);
//     }
// }

// @media (max-width: 600px) {
//     .shell {
//         transform: scale(0.4);
//     }
// }

.avatarList {
    width: 300px;
    height: 80px;
    // background-color: #4B70E2;
    display: flex;
    justify-content: space-between;
    align-items: center;

    img {
        width: 50px;
        height: 50px;
        border-radius: 30%;
        transition: all 0.3s ease-in-out;
    }

    .img-active {
        border: 5px solid #4B70E2;
        width: 60px;
        height: 60px;
        transition: all 0.3s ease-in-out;
    }
}

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    width: 600px;
    height: 100%;
    padding: 25px;
    background-color: #ecf0f3;
    transition: 1.25s;
}

.form {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.iconfont {
    margin: 0 5px;
    border: rgba(0, 0, 0, 0.5) 2px solid;
    border-radius: 50%;
    font-size: 25px;
    padding: 3px;
    opacity: 0.5;
    transition: 0.1s;
}

.iconfont:hover {
    opacity: 1;
    transition: 0.15s;
    cursor: pointer;
}

.form_input {
    width: 350px;
    height: 40px;
    margin: 4px 0;
    padding-left: 25px;
    font-size: 13px;
    letter-spacing: 0.15px;
    border: none;
    outline: none;
    background-color: #ecf0f3;
    transition: 0.25s ease;
    border-radius: 8px;
    box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
}

.form_input:focus {
    box-shadow: inset 4px 4px 4px #d1d9e6, inset -4px -4px 4px #f9f9f9;
}

.form_span {
    margin-top: 30px;
    margin-bottom: 12px;
}

.form_link {
    color: #181818;
    font-size: 15px;
    margin-top: 25px;
    border-bottom: 1px solid #a0a5a8;
    line-height: 2;
}

.title {
    font-size: 34px;
    font-weight: 700;
    line-height: 3;
    color: #181818;
    letter-spacing: 10px;
}

.description {
    font-size: 14px;
    letter-spacing: 0.25px;
    text-align: center;
    line-height: 1.6;
}

.button {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 1.15px;
    background-color: #4B70E2;
    color: #f9f9f9;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
    border: none;
    outline: none;
    cursor: pointer;
}

.a-container {
    z-index: 100;
    left: calc(100% - 600px);
}

.b-container {
    left: calc(100% - 600px);
    z-index: 0;
}

.switch {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 400px;
    padding: 50px;
    z-index: 200;
    transition: 1.25s;
    background-color: #ecf0f3;
    overflow: hidden;
    box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #d1d9e6;
}

.switch_circle {
    position: absolute;
    width: 500px;
    height: 500px;
    border-radius: 50%;
    background-color: #ecf0f3;
    box-shadow: inset 8px 8px 12px #b8bec7, inset -8px -8px 12px #fff;
    bottom: -60%;
    left: -60%;
    transition: 1.25s;
}

.switch_circle-t {
    top: -30%;
    left: 60%;
    width: 300px;
    height: 300px;
}

.switch_container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    position: absolute;
    width: 400px;
    padding: 50px 55px;
    transition: 1.25s;
}

.switch_button {
    cursor: pointer;
}

.switch_button:hover,
.submit:hover {
    box-shadow: 6px 6px 10px #d1d9e6, -6px -6px 10px #f9f9f9;
    transform: scale(0.985);
    transition: 0.25s;
}

.switch_button:active,
.switch_button:focus {
    box-shadow: 2px 2px 6px #d1d9e6, -2px -2px 6px #f9f9f9;
    transform: scale(0.97);
    transition: 0.25s;
}

.is-txr {
    left: calc(100% - 400px);
    transition: 1.25s;
    transform-origin: left;
}

.is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
}

.is-z {
    z-index: 200;
    transition: 1.25s;
}

.is-hidden {
    visibility: hidden;
    opacity: 0;
    position: absolute;
    transition: 1.25s;
}

.is-gx {
    animation: is-gx 1.25s;
}
.mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;

    .mask-content {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 300px;
        height: 200px;
        background: #fff;
        border-radius: 10px;
        padding: 20px;

        span{
            display: block;
            text-align: center;
            margin-top: 20px;
            line-height: 1.5;
        }

        .mask-btn {
            display: block;
            text-align: center;
            margin-top: 60px;
            
            button {
                padding: 5px 20px;
                border: 1px solid #1a1717;
                border-radius: 5px;
                color: #1a1717;
                margin: 0 10px;

            }

            .del{
                background-color: #ecb9b9;
            }

            button:hover {
                box-shadow: 0 0 5px #1a1717;
            }

            .del:hover {
                box-shadow: 0 0 5px #ee0505;
            }
        }
        
    }
}

@keyframes is-gx {

    0%,
    10%,
    100% {
        width: 400px;
    }

    30%,
    50% {
        width: 500px;
    }
}

.login-content-mobie {
    .shell {
        left: 0;
        transform: none;
        width: 100%;
        // margin-top: 100px;
        width: 100%;
        height: 500px;
        min-width: auto;
        min-height: auto;
    }

    .container {
        width: 100%;
    }

    .form_span {
        margin-top: 0;
    }

    .is-txl {
        transition: 2s;
    }
    .is-z {
        transition: 2s;
    }
}

.login {
    height: 600px;
    margin-bottom: 50px;
}

.mobie-toggle-btn {
    position: absolute;
    right: 10px;
    top: 10px;
    background-color: #4B70E2;
    border-radius: 5px;
    padding: 2px 5px;
    color: #fff;
}
</style>